﻿/// <reference path="../Scripts/MicrosoftAjax.debug.js" />
/// <reference path="../Scripts/MicrosoftAjaxApplicationServices.debug.js" />


(function () {
    var scriptName = "LoginBehavior";
    var e = null;

    function execute() {

        Type.registerNamespace("SCS");

        SCS.LoginBehavior = function (element) {
            SCS.LoginBehavior.initializeBase(this, [element]);

            this._redirect = "/";

            this._loginDelegate = null;
            this._validateDelegate = null;

            this._lazyValidate = true;
            this._root = null;
            this._host = null;
        }

        SCS.LoginBehavior.prototype = {
            initialize: function () {
                SCS.LoginBehavior.callBaseMethod(this, 'initialize');

                this._root = $get("loginForm");
                var root = this._root;

                var hostInfo;
                eval("hostInfo = " + $(root).attr("dialog-host"));
                this._host = $find(hostInfo.behaviorId),

                this._loginDelegate = Function.createDelegate(this, this._onLogin);
                $("#loginButton", root).bind("click", this._loginDelegate);

                this._validateDelegate = Function.createDelegate(this, this._onValidate);
                $("input:text, input:password", root).bind("blur.login", this._validateDelegate);
            },
            dispose: function () {

                //Sys.Debug.fail("");

                $("#loginButton", this._root).unbind("click");
                this._loginDelegate = null;

                $("input:text, input:password", this._root).unbind("blur.login");
                this._validateDelegate = null;

                SCS.LoginBehavior.callBaseMethod(this, 'dispose');
            },

            _onLogin: function () {

                if (this._validate()) {

                    var root = this._root;

                    Sys.Services.AuthenticationService.login(
                        $("#usernameEntry", root).val(),
                        $("#passwordEntry", root).val(),
                        ($("#rememberMeSelect", root).val() == "on"),
                        null,
                        this._redirect,
                        Function.createDelegate(this, this._onSucceededCall),
                        Function.createDelegate(this, this._onFailedCall),
                        "Login");
                }
            },

            _onFailedCall: function (result, context) {

                var msg = "";

                switch (context) {
                    case "Login":
                        msg = "Unable to login at this time. ";
                        break;
                }

                this._host.showMessage(msg, "error", ["OK"]);
            },

            _onSucceededCall: function (result, context) {
            
                switch (context) {
                    case "Login":
                        if (result)
                            this._host.showMessage("Login successful!", "success");
                        
                        else
                            this._host.showMessage("Login failed. Please try again.", "error", ["OK"]);                            
                        
                        break;
                }
            },

            _validate: function () {

                //                $.validity.setup({
                //                        outputMode: "modal",
                //                        modalErrorsClickable: false
                //                    });

                // Start validation:    
                $.validity.start();

                $("input:text, input:password", this._root).require();

                // End the validation session:    
                var result = $.validity.end();

                if (!result.valid) {
                    this._lazyValidate = false;
                    return false;
                }

                return true;
            },

            _onValidate: function (e) {

                if (!this._lazyValidate) {
                    this._validate();
                }
            }
        }
        SCS.LoginBehavior.registerClass('SCS.LoginBehavior', Sys.UI.Behavior);

        if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

    } // execute

    if (window.Sys && Sys.loader) {
        Sys.loader.registerScript(scriptName, null, execute);
    }
    else {
        execute();
    }

})();
